package com.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bean.WmsTUser;
import com.user.service.LoadService;
import com.user.util.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class Loadcontroller {

@Autowired
LoadService loadService;

@Resource
private RedisTemplate<String,Object> redisTemplate;


    @RequestMapping("scytest2.do")
    public String test2(HttpServletRequest request){
        ArrayList<String> FindsiteID = loadService.FindsiteID();
        for(int i=0;i<FindsiteID.size();i++){
            System.out.println(FindsiteID.get(i));
            String StoreID = loadService.FindstoreID(FindsiteID.get(i));
            System.out.println(StoreID);
            String storeName = loadService.FindstoreName(StoreID);
            System.out.println(storeName);
            ArrayList<String> StoreNameArrayList = new ArrayList<String>();
            StoreNameArrayList.add(storeName);
            request.getSession().setAttribute("StoreNameArrayList",StoreNameArrayList);
        }
        System.out.println("测试");
        return "user/Load";
    }

    @RequestMapping("scytest3.do")
    public String test3(HttpServletRequest request){

        List<String> FindtrayId = loadService.FindtrayId();
        request.getSession().setAttribute("FindtrayId",FindtrayId);
        System.out.println(FindtrayId);
        System.out.println("测试2");
        return "user/Load";
    }

    @RequestMapping("cangku.do")
    public String Find01(HttpServletRequest request){
      List<String> FindWarehouseName = loadService.FindWarehouseName();
      request.getSession().setAttribute("FindWarehouseName",FindWarehouseName);
        return "xitong/xitong_WarehouseUse";
    }

    @RequestMapping("FindWarehouseState.do")
    public String Find002(String WareHouseName,HttpServletRequest request){
        List<String> FindWarehouseState = loadService.FindWarehouseState(WareHouseName);
        for(int i=0;i<FindWarehouseState.size();i++){
            System.out.println(FindWarehouseState.get(i));
        }
        request.getSession().setAttribute("FindWarehouseState",FindWarehouseState);
       return  "xitong/xitong_WarehouseUse";
    }


    //进入登录页面 并且完成下拉框的赋值
    @RequestMapping("EnterLoad.do")
    public String EnterLoad(HttpServletRequest request){
        //调用查询登录页面的权限(下拉框
        List<String> PermissionName = loadService.QueryPermissions();
        request.getSession().setAttribute("PermissionName",PermissionName);
        return "user/Load";
    }

    //将已经登录成功的账号信息加入Redis
    public void redis(String userName,String password){
        //redis添加用户数据
        redisTemplate.opsForValue().set(userName,password);
    }

    //从Redis中查找是否有用户
    public WmsTUser Getredis(String userName,String password){
        //redis获取用户数据
        String WhetherHave = (String) redisTemplate.opsForValue().get(userName);
        System.out.println(WhetherHave);
        WmsTUser wmsTUser = (WmsTUser) JSONObject.parseObject(WhetherHave,WmsTUser.class);
        return wmsTUser;
    }

    //登录请求 判断权限 以及账号密码
    @RequestMapping("Load.do")
    public String load(String userAccount,String password,String permission,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        String SQLpermission =loadService.JudgePermission(userAccount);
        if(!SQLpermission.equals(permission)){
            return "user/Load";
        }
        else{
            if(Getredis(userAccount, password)!= null){
//                WmsTUser use1 = loadService.Load(userAccount,password);
                WmsTUser user1 = Getredis(userAccount,password);
                System.out.println("redis:"+user1);
                request.getSession().setAttribute("user",user1);
                return "xitong/newhome";
            }
            else {
                WmsTUser use1 = loadService.Load(userAccount,password);
                if(use1!= null){
                    System.out.println("redis"+use1);
                    request.getSession().setAttribute("user",use1);
                    String userstring = JSON.toJSONString(use1);
                    redis(userAccount,userstring);
                    return "xitong/newhome";
                }
                else {
                    return "user/Load";
                }
            }
        }
    }

    //验证码验证是否正确
    @RequestMapping("CodeValidation.do")
    public void yanzheng(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        System.out.println(code);
        HttpSession session = request.getSession();
        String code1 = (String) session.getAttribute("checkcode");
        System.out.println(code1);
        Boolean flag = false;
        if(code.equals(code1)){
            flag = true;
        }
        String flagString = JSON.toJSONString(flag);
        response.getWriter().print(flagString);
    }

    //验证码刷新
    @RequestMapping("CodeRefresh.do")
    protected void getCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CodeUtil codeUtil = new CodeUtil();
        codeUtil.getCode(request,response);
    }

}
